//
//  DKOrignalView.swift
//  Sina
//
//  Created by 赵柏清 on 16/8/4.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

/// 首页cell的间距
let HomeCellMargin = 10

class DKOrignalView: UIView {
    
    var constraint : Constraint?
    
    var orignalCellModel : DKHomeCellViewModel? {
        didSet {
            // 微博昵称
            nameLabel.text = orignalCellModel?.screen_name
            let iconURL = orignalCellModel?.statusModel?.user?.profile_image_url!
            
            let nsl = NSURL(string: iconURL!)
            // 头像图片
            photoImageView.sd_setImageWithURL(nsl!, placeholderImage: UIImage(named: "avatar_default_big"))
            // 认证图片
            verfitiedImageView.image = orignalCellModel?.verfited_Image
            // 等级图片
            levelImageView.image = orignalCellModel?.level_Image
            // 微博内容
            contentLabel.text = orignalCellModel?.statusModel?.text
            
            // 1.如果是原创微博有配图,则图片的地址信息放在原创微博的pic_urls字段里
            // 2.如果是转发微博则配图的信息放在转发(retweet_statu字段表示)微博的pic_urls字段里
            
            constraint?.uninstall()
            
            // 判断原创微博是不是有图片
            if let picsArray = orignalCellModel?.statusModel?.pic_urls where picsArray.count > 0 {
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                pictureView.pic_urls = picsArray
                
                layoutIfNeeded()
                

            } else {
                
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.constraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
            
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(verfitiedImageView)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin)
            
            make.width.height.equalTo(50)
        }
        
        verfitiedImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(HomeCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(10)
            make.top.equalTo(photoImageView.snp_bottom).offset(HomeCellMargin)
            make.right.equalTo(self.snp_right).offset(-10)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self).offset(HomeCellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
        }
        
        // 告诉self它的底部在哪里,这样才能自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            self.constraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    
    // MARK: 懒加载
    /// 头像,先设置的占位图
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
    }()
    
    /// 认证图片
    private lazy var verfitiedImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_enterprise_vip")
        
        return img
    }()
    
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "名字"
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        
        
        
        return label
    }()
    
    /// 等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
    ///时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "时间"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.orangeColor()
        
        
        return label
    }()
    
    ///来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "来源"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    ///内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "文件而反过来看就按了沙发了开始进而发生看来减肥"
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.blackColor()
        
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 原创微博的照片
    private lazy var pictureView: DKPhotoView = DKPhotoView()

}
